package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOrdinaryCargoPublishCarInfoBinding;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.PickerHelper;
import com.sgb.kjwl.utils.RangeFocusChangeListener;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.widget.CarTypeAndLenPopWindow;
import com.sgb.kjwl.viewmodel.transport.CargoPublishCarInfoViewModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.Arrays;

import javax.inject.Inject;

/**
 * @author: 杨畔
 * @date: 2021/6/22 15:05
 * @Description: 普通货源发布填写车辆信息页面
 */
public class OrdinaryCargoPublishCarInfoActivity extends AppActivity {

    @Inject
    CargoPublishCarInfoViewModel viewModel;
    ActivityOrdinaryCargoPublishCarInfoBinding binding;
    //选择器
    private CarTypeAndLenPopWindow mCarInfoPop;
    private OptionsPickerView<String> mDumpPicker;
    //数据实体类
    private CargoPublishEntity mEntity;

    public static void start(Activity activity, CargoPublishEntity entity, int requestCode) {
        Intent intent = new Intent(activity, OrdinaryCargoPublishCarInfoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(RouterKey.DATA, entity);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        viewModel = new ViewModelProvider(this).get(CargoPublishCarInfoViewModel.class);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_ordinary_cargo_publish_car_info);
        binding.setLifecycleOwner(this);
        binding.includeTitle.titleView.setText("车辆要求");
        initPicker();
        initEvent();
        initData();
    }

    private void initPicker() {
        mCarInfoPop = new CarTypeAndLenPopWindow(this);
        mCarInfoPop.addCallback((carTypeList, carLenList) -> {
            mEntity.setCarTypeList(carTypeList);
            mEntity.setCarLenList(carLenList);
            setCarInfoContent();
        });
        mDumpPicker = PickerHelper.with(this).getCommonPicker("选择是否自卸", Arrays.asList("非自卸", "自卸"), (options1, options2, options3, v) -> {
            mEntity.setIsDump(options1);
            binding.tvReleaseCargoDump.setText(options1 == 1 ? "自卸" : "非自卸");
        });
    }

    private void initEvent() {
        binding.setOnClick(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
        //设置输入范围，失去焦点后自动补全
        binding.etReleaseCargoHeight.setOnFocusChangeListener(new RangeFocusChangeListener(0.01, 999.99));
        binding.rgReleaseCargoType.setOnCheckedChangeListener((group, checkedId) -> {
            //设置用车类型
//            mEntity.setUseCarType(checkedId == R.id.rb_release_cargo_wv ? 1 : 0);

        });
        //接口回调
        viewModel.ldCarType.observe(this, list -> {
            mCarInfoPop.setCarTypeData(list);
        });
        viewModel.ldCarLen.observe(this, list -> {
            mCarInfoPop.setCarLenData(list);
        });
    }

    private void initData() {
        mEntity = getIntent().getParcelableExtra(RouterKey.DATA);
        if (mEntity == null) {
            mEntity = new CargoPublishEntity();
        } else {
            syncData();
        }
        //获取车型车长
        viewModel.getCarTypeAndLen(this, DefaultUtil.getString(mEntity.getGoodsModeNo()));
    }

    private void syncData() {
        //设置用车类型
        if (mEntity.getUseCarType() == 1){
            binding.rgReleaseCargoType.check(R.id.rb_release_cargo_wv);
        }else {
            binding.rgReleaseCargoType.check(R.id.rb_release_cargo_ltl);
        }
        setCarInfoContent();
        if (mEntity.getHeightHurdle() != 0) {
            binding.etReleaseCargoHeight.setText(NumberUtil.trim0(mEntity.getHeightHurdle()));
        }
        if (mEntity.getIsDump() != null) {
            binding.tvReleaseCargoDump.setText(mEntity.getIsDump() == 1 ? "自卸" : "非自卸");
        }
    }

    /**
     * 设置车辆信息内容
     */
    private void setCarInfoContent() {
        if (mEntity.getCarTypeList() == null || mEntity.getCarLenList() == null) {
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (GoodsInfoTypeEntity3 type : mEntity.getCarTypeList()) {
            builder.append("/").append(type.getName());
        }
        for (GoodsInfoTypeEntity3 type : mEntity.getCarLenList()) {
            builder.append("/").append(type.getName());
        }
        if (!TextUtils.isEmpty(builder.toString())) {
            binding.tvReleaseCargoCarInfo.setText(builder.substring(1));
        }
    }

    @Override
    public void onClick(View v) {
        closeKeybord();
        if (v == binding.includeTitle.ivBack) {
            finish();
        } else if (v == binding.llReleaseCargoCarInfo) {
            showCarInfoPop();
        } else if (v == binding.llReleaseCargoDump) {
            mDumpPicker.show();
        } else if (v == binding.tvReleaseCargoSubmit) {
            clearAllFocus();
            checkData();
        }
    }

    /**
     * 展示车辆弹窗
     */
    private void showCarInfoPop() {
        //设置默认车型
        if (mEntity.getCarTypeList() != null && !mEntity.getCarTypeList().isEmpty()) {
            mCarInfoPop.setSelectedCarType(mEntity.getCarTypeList());
        }
        //设置默认车长
        if (mEntity.getCarLenList() != null && !mEntity.getCarLenList().isEmpty()) {
            mCarInfoPop.setSelectedCarLen(mEntity.getCarLenList());
        }
        mCarInfoPop.backgroundAlpha(0.5f);
        mCarInfoPop.showAtLocation(binding.getRoot(), Gravity.BOTTOM, 0, 0);
    }

    private void checkData() {
        if (mEntity == null) return;
        //判断车型车长
        if (mEntity.getCarTypeList() == null || mEntity.getCarTypeList().isEmpty()) {
            MToast.showToast(this, "请输入车型");
            return;
        }
        //判断车长
        if (mEntity.getCarLenList() == null || mEntity.getCarLenList().isEmpty()) {
            MToast.showToast(this, "请输入车长");
            return;
        }
        //设置栏高
        if (binding.etReleaseCargoHeight.getText() != null) {
            mEntity.setHeightHurdle(NumberUtil.convertToDouble(binding.etReleaseCargoHeight.getText().toString()));
        } else {
            mEntity.setHeightHurdle(0);
        }
        Intent intent = new Intent();
        intent.putExtra(RouterKey.DATA, mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }
}
